import {
  getPostByDiscussId,
  getPostByTopic,
  getPostCommentList,
  getPostDetail,
  getPostList,
  getPostListByUid,
  deleteComment,
  postAddArticle,
  postEditArticle,
  getRemainComment,
  getFollowUserPostList,
  getHotPostList,
  getFollowPostList,
  getReportPostList
} from '@/axios/api/posts'
import { getDiscussDetail } from '@/axios/api/topic'
import { isLastPage } from '@/utils/dataUtils'
import { useInfiniteQuery, useQuery, useMutation } from '@tanstack/vue-query'
import { getToken } from '@/utils/webStorage'
import { formatText } from '@/utils/transIUtils'
export const useLastPostListQuery = (filter) => {
  return useInfiniteQuery({
    queryKey: ['lastPost', filter],
    queryFn: getPostList,
    initialPageParam: 1,
    getNextPageParam: (lastPage) => {
      // 后端bug，只能用length判断
      if (lastPage.result?.data.length === 0) {
        return undefined
      }
      return lastPage.result?.current_page + 1
    },
  })
}

// 获取我关注的用户的帖子
export const useFollowUserPostListQuery = () => {
  return useInfiniteQuery({
    queryKey: ['followUserPost'],
    queryFn: getFollowUserPostList,
    getNextPageParam: (lastPage) => {
      if (lastPage.result?.data.length === 0) {
        return undefined
      }
      return lastPage.result?.current_page + 1
    },
  })
}

// 用户个人中心的发帖
export const useUserPostsQuery = (uid) => {
  return useInfiniteQuery({
    queryKey: ['userPosts', uid],
    queryFn: getPostListByUid,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
    enabled: !!getToken(),
  })
}

// 用户个人中心的发帖
export const useReportPostsQuery = (status) => {
  debugger
  return useInfiniteQuery({
    queryKey: ['useReport', status],
    queryFn: getReportPostList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}

export const useUserPostsDataQuery = (uid) => {
  return useInfiniteQuery({
    queryKey: ['userPostsData', uid],
    queryFn: getFollowPostList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
    enabled: !!getToken(),
  })
}

export const usePostByTopicQuery = (topicId, sortFilter) => {
  return useInfiniteQuery({
    queryKey: ['postByTopic', topicId, sortFilter],
    queryFn: getPostByTopic,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}

export const usePostByDiscussQuery = (discussId, sortFilter) => {
  return useInfiniteQuery({
    queryKey: ['postByDiscuss', discussId, sortFilter],
    queryFn: getPostByDiscussId,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}

export const useDiscussDetailQuery = (discussId) => {
  return useQuery({
    queryKey: ['discussDetail', discussId],
    queryFn: getDiscussDetail,
    enabled: computed(() => discussId.value !== null),
  })
}

export const usePostDetailQuery = (postId) => {
  return useQuery({
    queryKey: ['postDetail', postId],
    queryFn: getPostDetail,
    // 使用 select 选项预处理数据
    select: (data) => {
      // 这里可以对 data 进行任何转换处理
      return {
        ...data,
        result: {
          ...data.result,
          content: formatText(data.result.content.replace(/\n/g, '<br>')),
        },
      }
    },
    // enabled: !!postId.value,
  })
}

export const usePostCommentQuery = (postId) => {
  return useInfiniteQuery({
    queryKey: ['postComment', postId],
    queryFn: getPostCommentList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}

export const useDeleteCommentMutation = () => {
  return useMutation({
    mutationFn: deleteComment,
  })
}

export const useAddArticleMutation = () => {
  return useMutation({
    mutationFn: postAddArticle,
  })
}

export const useEditArticleMutation = () => {
  return useMutation({
    mutationFn: postEditArticle,
  })
}

export const useRemainCommentQuery = (commentId) => {
  return useQuery({
    queryKey: ['remainComment', commentId],
    queryFn: getRemainComment,
    enabled: false,
  })
}

// 获取今日热门帖子
// export const useHotPostQuery = () => {
//   return useQuery({
//     queryKey: ['hotPost'],
//     queryFn: getHotPostList,
//   })
// }

export const useHotPostQuery = (postId) => {
  console.log(postId)
  return useInfiniteQuery({
    queryKey: ['hotPost', postId],
    queryFn: getHotPostList,
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.result.current_page + 1
    },
  })
}